package com.iphone.extra.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class ShortcutWidgetLayout extends LinearLayout {

    public static final String ACTION = "com.extra.action.CHECK_UNLOCK";

    private Runnable mRunnable;

    public ShortcutWidgetLayout(Context context) {
        super(context);
    }

    public ShortcutWidgetLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ShortcutWidgetLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        IntentFilter filter = new IntentFilter(ACTION);
        getContext().registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        getContext().unregisterReceiver(mReceiver);
    }

    public void setOnShortcutClickCallback(Runnable runnable) {
        mRunnable = runnable;
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (mRunnable != null) {
                mRunnable.run();
            }
        }
    };
}
